package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;

import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //添加套餐
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){

        setmealService.addSetmeal(setmealDto);

        return R.success(null);
    }
    //套餐分页查询
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){

        Page<Setmeal> p =  setmealService.findPage(page,pageSize,name);

        return R.success(p);
    }

    //批量删除套餐
    @DeleteMapping
    public R deleteIds(Long[] ids){
        setmealService.deleteIds(ids);

        return R.success(null);
    }

    //套餐数据回显
    @GetMapping("{id}")
    public R findById(@PathVariable Long id){

     SetmealDto setmealDto =  setmealService.selectById(id);

        return R.success(setmealDto);
    }

    //套餐修改
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){

        setmealService.updateSetmeal(setmealDto);

        return R.success(null);
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){


      List<Setmeal> list =   setmealService.findStmeals(setmeal);

        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R details(@PathVariable Long id){

        List<SetmealDish>  setmealDishes = setmealService.getDishs(id);

        return R.success(setmealDishes);

    }
}
